// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraGameSettingRegistry.h"

#include "GameSettingCollection.h"
#include "EditCondition/WhenPlayingAsPrimaryPlayer.h"
#include "EditCondition/WhenPlatformHasTrait.h"
#include "CustomSettings/LyraSettingValueDiscrete_Language.h"
#include "LyraSettingsLocal.h"
#include "GameSettingValueDiscreteDynamic.h"
#include "Player/LyraLocalPlayer.h"
#include "Replays/LyraReplaySubsystem.h"

#define LOCTEXT_NAMESPACE "Lyra"

/**
 * 初始化游戏玩法设置
 * @param InLocalPlayer 本地玩家
 * @return 游戏设置集合
 */
UGameSettingCollection* ULyraGameSettingRegistry::InitializeGameplaySettings(ULyraLocalPlayer* InLocalPlayer)
{
	UGameSettingCollection* Screen = NewObject<UGameSettingCollection>(); // 创建游戏设置集合
	Screen->SetDevName(TEXT("GameplayCollection")); // 设置开发名称
	Screen->SetDisplayName(LOCTEXT("GameplayCollection_Name", "Gameplay")); // 设置显示名称
	Screen->Initialize(InLocalPlayer); // 初始化

	{
		UGameSettingCollection* LanguageSubsection = NewObject<UGameSettingCollection>(); // 创建语言设置子部分
		LanguageSubsection->SetDevName(TEXT("LanguageCollection")); // 设置开发名称
		LanguageSubsection->SetDisplayName(LOCTEXT("LanguageCollection_Name", "Language")); // 设置显示名称
		Screen->AddSetting(LanguageSubsection); // 添加到屏幕设置

		//----------------------------------------------------------------------------------
		{
			ULyraSettingValueDiscrete_Language* Setting = NewObject<ULyraSettingValueDiscrete_Language>(); // 创建语言离散值设置
			Setting->SetDevName(TEXT("Language")); // 设置开发名称
			Setting->SetDisplayName(LOCTEXT("LanguageSetting_Name", "Language")); // 设置显示名称
			Setting->SetDescriptionRichText(LOCTEXT("LanguageSetting_Description", "The language of the game.")); // 设置描述富文本
			
#if WITH_EDITOR
			if (GIsEditor) // 如果在编辑器中
			{
				Setting->SetDescriptionRichText(LOCTEXT("LanguageSetting_WithEditor_Description", "The language of the game.\n\n<text color=\"#ffff00\">WARNING: Language changes will not affect PIE, you'll need to run with -game to test this, or change your PIE language options in the editor preferences.</>")); // 设置编辑器中的描述富文本
			}
#endif
			
			Setting->AddEditCondition(FWhenPlayingAsPrimaryPlayer::Get()); // 添加编辑条件

			LanguageSubsection->AddSetting(Setting); // 添加到语言子部分
		}
		//----------------------------------------------------------------------------------
	}

	{
		UGameSettingCollection* ReplaySubsection = NewObject<UGameSettingCollection>(); // 创建回放设置子部分
		ReplaySubsection->SetDevName(TEXT("ReplayCollection")); // 设置开发名称
		ReplaySubsection->SetDisplayName(LOCTEXT("ReplayCollection_Name", "Replays")); // 设置显示名称
		Screen->AddSetting(ReplaySubsection); // 添加到屏幕设置

		//----------------------------------------------------------------------------------
		{
			UGameSettingValueDiscreteDynamic_Bool* Setting = NewObject<UGameSettingValueDiscreteDynamic_Bool>(); // 创建布尔离散值设置
			Setting->SetDevName(TEXT("RecordReplay")); // 设置开发名称
			Setting->SetDisplayName(LOCTEXT("RecordReplaySetting_Name", "Record Replays")); // 设置显示名称
			Setting->SetDescriptionRichText(LOCTEXT("RecordReplaySetting_Description", "Automatically record game replays. Experimental feature, recorded demos may have playback issues.")); // 设置描述富文本

			Setting->SetDynamicGetter(GET_LOCAL_SETTINGS_FUNCTION_PATH(ShouldAutoRecordReplays)); // 设置动态获取器
			Setting->SetDynamicSetter(GET_LOCAL_SETTINGS_FUNCTION_PATH(SetShouldAutoRecordReplays)); // 设置动态设置器
			Setting->SetDefaultValue(GetDefault<ULyraSettingsLocal>()->ShouldAutoRecordReplays()); // 设置默认值

			Setting->AddEditCondition(FWhenPlayingAsPrimaryPlayer::Get()); // 添加编辑条件
			Setting->AddEditCondition(FWhenPlatformHasTrait::KillIfMissing(ULyraReplaySubsystem::GetPlatformSupportTraitTag(), TEXT("Platform does not support saving replays"))); // 添加编辑条件

			ReplaySubsection->AddSetting(Setting); // 添加到回放子部分

		}
		//----------------------------------------------------------------------------------

		//----------------------------------------------------------------------------------
		{
			UGameSettingValueDiscreteDynamic_Number* Setting = NewObject<UGameSettingValueDiscreteDynamic_Number>(); // 创建数字离散值设置
			Setting->SetDevName(TEXT("KeepReplayLimit")); // 设置开发名称
			Setting->SetDisplayName(LOCTEXT("KeepReplayLimitSetting_Name", "Keep Replay Limit")); // 设置显示名称
			Setting->SetDescriptionRichText(LOCTEXT("KeepReplayLimitSetting_Description", "Number of saved replays to keep, set to 0 for infinite.")); // 设置描述富文本

			Setting->SetDynamicGetter(GET_LOCAL_SETTINGS_FUNCTION_PATH(GetNumberOfReplaysToKeep)); // 设置动态获取器
			Setting->SetDynamicSetter(GET_LOCAL_SETTINGS_FUNCTION_PATH(SetNumberOfReplaysToKeep)); // 设置动态设置器
			Setting->SetDefaultValue(GetDefault<ULyraSettingsLocal>()->GetNumberOfReplaysToKeep()); // 设置默认值
			for (int32 Index = 0; Index <= 20; Index++) // 遍历0-20
			{
				Setting->AddOption(Index, FText::AsNumber(Index)); // 添加选项
			}

			Setting->AddEditCondition(FWhenPlayingAsPrimaryPlayer::Get()); // 添加编辑条件
			Setting->AddEditCondition(FWhenPlatformHasTrait::KillIfMissing(ULyraReplaySubsystem::GetPlatformSupportTraitTag(), TEXT("Platform does not support saving replays"))); // 添加编辑条件

			ReplaySubsection->AddSetting(Setting); // 添加到回放子部分

		}
		//----------------------------------------------------------------------------------
	}

	return Screen; // 返回屏幕设置集合
}

#undef LOCTEXT_NAMESPACE